package mypackage;

import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
    private static RichTextField _logField = new RichTextField();
    
    private static long _tStart;
    
    /**
     * Creates a new MyScreen object
     */
    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("MyTitle");
        
        ButtonField startButton = new ButtonField("start");
        startButton.setRunnable(new Runnable()
        {
            public void run()
            {
                startFullDuplex();
            }
        });
        add(startButton);
        
        _tStart = System.currentTimeMillis();
        Font f = _logField.getFont();
        _logField.setFont(f.derive(f.getStyle(), f.getHeight() * 3 / 5));
        add(_logField);
    }

    private void startFullDuplex()
    {
        MyScreen.log("start play");
        new PlayerClient().start();
        
        MyScreen.log("start record");
        //new RecordClient().start();
        
    }
    
    public static void log(final String sValue)
    {
        UiApplication.getUiApplication().invokeLater(new Runnable()
        {
            public void run()
            {
                long t = System.currentTimeMillis() - _tStart;
                if (true)
                {
                    _logField.insert("[" + ((t / 1000) + "." + t % 1000) + "] "
                        + sValue + "\n");
                }
            }
        });
    }
}
